package org.jcms.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jcms.model.Customer;
import org.jcms.service.CustomerServiceI;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class AdminBatchAdd extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public AdminBatchAdd() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(this.getServletContext());
		CustomerServiceI customerServiceI = (CustomerServiceI) ac
				.getBean("customerServiceImpl");
		String retJson = "{\"error\":@err,\"msg\":\"@msg\"}";
		String err = "1";
		String msg = "";
		String manyCustomerId = request.getParameter("manyCustomerId");
		String type = request.getParameter("type");
		int allStu = 0;
		int successStu = 0;
		try {
			int Itype = Integer.parseInt(type);
			manyCustomerId = manyCustomerId.trim();
			manyCustomerId = manyCustomerId.replaceAll("\r", "");
			if (Itype != 0 && Itype != 1) {
				msg = "用户类型错误";
				return;
			}
			if (manyCustomerId == null || "".equals(manyCustomerId)) {

				msg = "用户信息不能为空";
			}

			else {

				String customerIds[] = manyCustomerId.split("\n");
				allStu = customerIds.length;
				for (int i = 0; i < customerIds.length; i++) {
					// System.out.println(stuIds[i]);
					if (customerIds[i] == null || "".equals(customerIds[i])) {

						msg += "<br><span style='color:red'>第" + (i + 1)
								+ "个用户id为空;</span>";
					}

					else if (customerServiceI.findById(customerIds[i]) != null) {

						msg += ("<br><span style='color:red'>第" + (i + 1)
								+ "个用户名'" + customerIds[i] + "'已经存在;</span> ");
					}

					else {

						Customer customer = new Customer();
						customer.setCustomerId(customerIds[i]);
						customer.setCustomerType(Itype);
						customer.setCustomerIsBlocked(0);
						customer.setCustomerName(customerIds[i]);
						customer.setCustomerPassword(customerIds[i]);
						customerServiceI.create(customer);
						err = "0";
						successStu++;

					}
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			msg += "<br>系统出错！";
			e.printStackTrace();
		} finally {

			msg = ("<br>总共提交用户数量: " + allStu + "<br>已成功导入的用户数量: " + successStu + "<br>")
					+ msg;
			retJson = retJson.replace("@err", err);
			retJson = retJson.replace("@msg", msg);
			response.getOutputStream().write(retJson.getBytes("utf-8"));
		}

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
